<template>
    <view class="page-wrap">
        <Position :info="workDetail" :type="type" :showRemark="false"></Position>
        <view class="content">
            <view class="network">
                <view class="item-title">
                    <view class="title">
                        <view class="txt">
                            服务性质
                        </view>
                        <view class="bg"></view>
                    </view>
                    <UploadMore v-if="form['服务性质'].needFile" :uploadParams="{ ...uploadParams, type_name: '服务性质' }"
                        :text="'维修图片'" :title="'维修图片'" :fileList="form['服务性质'].fileList"
                        @changeBefore="(val) => fileChangeBefore(val, '服务性质')"
                        @changeDoing="(val) => fileChangeDoing(val, '服务性质')"
                        @changeComplete="(val) => fileChangeComplete(val, '服务性质')">
                    </UploadMore>
                </view>

                <u-radio-group placement="column" v-model="form['服务性质']['服务']" @change="changeRadio">
                    <view class="child">
                        <view class="child-title">
                            <view class="title">
                                <u-radio name="保修服务" label="保修服务"></u-radio>
                            </view>
                        </view>
                    </view>

                    <view class="child">
                        <view class="child-title">
                            <view class="title">
                                <u-radio name="收费服务" label="收费服务"></u-radio>
                            </view>
                        </view>
                        <view class="fileds">
                            <input :disabled="form['服务性质']['服务']=='保修服务'" class="cinput" type="text" v-model="form['服务性质']['金额']" placeholder="请输入金额">
                        </view>
                    </view>
                </u-radio-group>

            </view>


            <view class="network">
                <view class="item-title">
                    <view class="title">
                        <view class="txt">
                            设备情况
                        </view>
                        <view class="bg"></view>
                    </view>
                    <ImgUpload v-if="form['设备情况'].needFile" :title="'设备情况'" :fileList="form['设备情况'].fileList"
                        @change="(val) => fileChange(val, '设备情况')">
                    </ImgUpload>
                </view>
                <view class="child">
                    <view class="child-title">
                        <view class="title">维修</view>
                    </view>
                    <view class="fileds">
                        <input class="cinput" type="text" v-model="form['设备情况']['维修']" placeholder="请输入">
                    </view>
                </view>

                <view class="child">
                    <view class="child-title">
                        <view class="title">替代</view>
                    </view>
                    <view class="fileds">
                        <input class="cinput" type="text" v-model="form['设备情况']['替代']" placeholder="请输入">
                    </view>
                </view>
            </view>

            <view class="network" v-for="(key, index) in formKeys" :key="index">
                <view class="item-title">
                    <view class="title">
                        <view class="txt">
                            {{ key }}
                        </view>
                        <view class="bg"></view>
                    </view>
                    <ImgUpload v-if="form[key].needFile" :title="key" :fileList="form[key].fileList"
                        @change="(val) => fileChange(val, key)">
                    </ImgUpload>
                </view>
                <view class="child">
                    <textarea :maxlength="-1"  v-model="form[key].value" class="my-textarea" placeholder="请输入"></textarea>
                </view>
            </view>

            <view class="h1" style="height: 1px ;"></view>
        </view>
        <view class="btns" v-if="!isDetail">
            <view class="btn save" @tap="submit(1)">存草稿</view>
            <view class="btn sub" @tap="submit(0)">确认提交</view>
        </view>
    </view>
</template>

<script>

import { patrolAdd, getWorkDetail, getWorkInfo } from '@/api'
export default {

    computed: {

        workType() {
            let map = {
                1: "网点",
                3: "安防",
                2: "基站",
            }
            return map[this.type]
        },

    },
    data() {
        return {
            isDetail:'',
            remark: '',
            formKeys: ['维修地址', '甲方要求及故障现象描述', '诊断结果', '解决方案及方法', '备注'],
            form: {
                '服务性质': {
                    fileList: {
                        before: [],
                        doing: [],
                        complete: []
                    },
                    needFile: true,
                    '服务': '保修服务',
                },
                '设备情况': {
                    '维修': '',
                    '替代': '',
                },
                '维修地址': {
                    value: ''
                },
                '甲方要求及故障现象描述': {
                    value: ''
                }, '诊断结果': {
                    value: ''
                }, '解决方案及方法': {
                    value: ''
                },
                '备注': {
                    value: ''
                },
            },
            remark: '',
            uploadParams: {},
            workInfo: null,
            workDetail: null,
            type: '1',
            defaultValue: null,
        }
    },
    onLoad(options) {
        console.log(options)
        const { type, id, pid, mid ,isDetail} = options
        this.type = type
        this.isDetail=isDetail
        this.workInfo = {
            type, id, pid, mid
        }
        this.uploadParams = {
            type, id, pid, mid,
            wid: this.wid
        }
        this.getWorkInfo(this.workInfo)
        this.getWorkDetail(this.workInfo)
    },
    onShow(){
        this.getWorkInfo(this.workInfo) 
    },
    methods: {
        async getWorkDetail(params) {
            if (params.id != '0') {
                let res = await getWorkDetail({
                    id: params.id,
                    wid: this.wid,
                    type: '2'
                })
                if (res.formdata) {
                    this.defaultValue = JSON.parse(res.formdata)
                }
            }
        },
        async getWorkInfo(params) {
            let res = await getWorkInfo({
                wid: this.wid,
                mid: params.mid
            })
            this.workDetail = res
        },
        fileChangeBefore(val, key = '') {
            this.form[key].fileList.before = val
        },
        fileChangeDoing(val, key = '') {
            this.form[key].fileList.doing = val
        },
        fileChangeComplete(val, key = '') {
            this.form[key].fileList.complete = val
        },
        getForm(val) {
            this.form = val;
        },
        submit(draft = 0) {
            wx.showModal({
                title: '提示',
                content: '是否确定提交全部工单和附件图片？',
                success: async (res) => {
                    if (res.confirm) {
                        console.log('用户点击确定', JSON.stringify(this.form))
                        let params = {
                            ...this.workInfo,
                            ac: 'maintain_add',
                            formdata: JSON.stringify({
                                ...this.form,
                            }),
                            wid: this.wid,
                            draft
                        }
                        if (params.id == '0') {
                            delete params.id
                        }
                        await patrolAdd(params)
                        wx.showToast({
                            title: draft ? '保存至草稿成功' : '工单提交成功'
                        })
                        setTimeout(() => {
                            wx.navigateBack()
                        }, 1000)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        },
        changeRadio(val){
            console.log(val)
            if(val=='保修服务'){
                this.form['服务性质']['金额']=''
            }
        }
    },
    watch: {
        defaultValue: {
            handler(val) {
                this.form = val
            },
        }
    }
}
</script>

<style src="./assets/index.scss" lang="scss" scoped></style>


